
package jing.upnp.ssdp.type.usn;

/*
 *  -----------------------------------------------------------------------------------
 *  URI
 *  -----------------------------------------------------------------------------------
 *  upnp:rootdevice                                         - Root Device
 *  uuid:[Device UUID]                                      - Device unique identifier
 *  urn:schemas-upnp-org:device:[Device Type]:[Version]     - UPnP Device Type
 *  urn:schemas-upnp-org:service:[Service Type]:[Version]   - UPnP Service Type
 *  urn:[Domain Name]:device:[Device Type]:[Version]        - Non-UPnP Device Type
 *  urn:[Domain Name]:service:[Service Type]:[Version]      - Non-UPnP Service Type
 *
 *  @note
 *      DLNA Architecture, Page 64
 */
public class Uri implements Comparable<Uri> {

    public static final String URI_ROOT_DEVICE = "upnp:rootdevice";
    public static final String URI_UUID = "uuid";
    public static final String URI_URN = "urn";
    public static final String URI_UPNP = "schemas-upnp-org";
    public static final String URI_DEVICE = "device";
    public static final String URI_SERVICE = "service";

    public enum Type {
        Unknown,
        RootDevice,
        DeviceUUID,
        UPnPDevice,
        UPnPService,
        NonUPnPDevice,
        NonUPnPService,
    }

    private Type type = Type.Unknown;

    // for DeviceUUID
    private String uuid = null;

    // for NonUPnPDevice & NonUPnPService
    private String domainName = null;

    // for Device
    private String deviceType = null;
    private String deviceVersion = null;

    // for Service
    private String serviceType = null;
    private String serviceVersion = null;


    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getDeviceVersion() {
        return deviceVersion;
    }

    public void setDeviceVersion(String deviceVersion) {
        this.deviceVersion = deviceVersion;
    }

    public String getServiceType() {
        return serviceType;
    }

    public void setServiceType(String serviceType) {
        this.serviceType = serviceType;
    }

    public String getServiceVersion() {
        return serviceVersion;
    }

    public void setServiceVersion(String serviceVersion) {
        this.serviceVersion = serviceVersion;
    }

    @Override
    public String toString() {

        String string = null;

        switch (this.type) {
            case RootDevice:
                string = URI_ROOT_DEVICE;
                break;

            case DeviceUUID:
                string = String.format("%s:%s", URI_UUID, this.uuid);
                break;

            case UPnPDevice:
                string = String.format("%s:%s:%s:%s:%s",
                        URI_URN,
                        URI_UPNP,
                        URI_DEVICE,
                        this.deviceType,
                        this.deviceVersion);
                break;

            case UPnPService:
                string = String.format("%s:%s:%s:%s:%s",
                        URI_URN,
                        URI_UPNP,
                        URI_SERVICE,
                        this.serviceType,
                        this.serviceVersion);
                break;

            case NonUPnPDevice:
                string = String.format("%s:%s:%s:%s:%s",
                        URI_URN,
                        this.domainName,
                        URI_DEVICE,
                        this.deviceType,
                        this.deviceVersion);
                break;

            case NonUPnPService:
                string = String.format("%s:%s:%s:%s:%s",
                        URI_URN,
                        this.domainName,
                        URI_SERVICE,
                        this.serviceType,
                        this.serviceVersion);
                break;

            case Unknown:
                string = "null";
                break;

            default:
                string = "null";
                break;
        }

        return string;
    }

    @Override
    public int compareTo(Uri another) {
        return this.toString().compareToIgnoreCase(another.toString());
    }
}